const App = {
    data() {
        return {
            option: {
                navIsShrink: false,
                pcShrink: false
            },
            meter: {
                search: '',
                itemsCount: 0,
                tableDatas: []
            },
            userSafe: {
                updatePassword: {
                    OriginalPassword: '',
                    NewPassword: '',
                    ConfirmPassword: ''
                },
                updateMessage: {
                    UserNickName: '',
                    UserEmail: '',
                    UserName: ''
                },
            },
            addCreditCard: {
                CreditCardNum: '',
                CreditCardPrice: 0,
                CreditCardTime: new Date(),
                BankId: 1,
            },
            updateCreditCard: {
                isShow: false,
                CreditCardId: '',
                CreditCardNum: '',
                CreditCardPrice: 0,
                CreditCardTime: '',
                BankId: '',
                isPay: ''
            },
            manageCreditCard: {
                search: '',
                bankId: '',
                isPay: '',
                rangeTime: ['', ''],
                itemsCount: 0,
                tableDatas: []
            },
            BankMsgAll: [],

        };
    },
    methods: {
        IsShrink: function () {
            this.option.navIsShrink = !this.option.navIsShrink;
            this.option.pcShrink = !this.option.pcShrink;
        },
        tipsMessage: function (code, message) {
            let type = '';
            if (code === 0) {
                type = 'success'
            } else if (code === 1) {
                type = 'info'
            } else if (code === 2) {
                type = 'warning'
            } else if (code === 3) {
                type = 'error'
            }
            this.$message({
                type: type,
                message: message
            });
        },
        updatePassword: function () {
            const updatePassword = this.userSafe.updatePassword;
            const oldPassword = updatePassword.OriginalPassword;
            const newPassword = updatePassword.NewPassword;
            const cpassword = updatePassword.ConfirmPassword;
            if (isEmpty(oldPassword, newPassword, cpassword)) {
                this.tipsMessage(2, "请不要留空");
                return;
            }
            if (cpassword !== newPassword) {
                this.tipsMessage(2, "请确认密码是否输入正确");
                return;
            }
            axios.post("/api/User/isLogin/UpdateUserMsgServlet", {
                state: 1,
                oldPassword: oldPassword,
                newPassword: newPassword
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    return
                }
                this.tipsMessage(data.code, data.message);
                setTimeout(() => {
                    location.reload();
                }, 500);
            });
        },//修改密码
        updateUserMsg: function () {
            const userMsg = this.userSafe.updateMessage;
            const nickname = userMsg.UserNickName;
            const email = userMsg.UserEmail;
            const username = userMsg.UserName;
            if (isEmpty(nickname, email, username)) {
                this.tipsMessage(2, "请不要留空")
                return;
            }
            if (isEmail(email)) {
                this.tipsMessage(1, "您输入的邮箱格式有误");
                return;
            }
            axios.post("/api/User/isLogin/UpdateUserMsgServlet", {
                state: 2,
                nickname: nickname,
                email: email,
                username: username
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    return;
                }
                this.tipsMessage(data.code, data.message);
                setTimeout(() => {
                    location.reload()
                }, 500);
            });
        },//修改与用户数据
        loadUserMsg: async function () {
            await axios.get("/api/User/isLogin/UserMsgServlet").then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    location.reload();
                    return;
                }
                const message = data.message;
                this.userSafe.updateMessage.UserEmail = message.userEmail;
                this.userSafe.updateMessage.UserName = message.userName;
                this.userSafe.updateMessage.UserNickName = message.userNickname;
            })
        },//加载用户信息
        loadBankMsg: async function () {
            await axios.get("/api/Bank/BankMsgServlet").then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    location.reload();
                    return;
                }
                this.BankMsgAll = data.message;
            });
        },//获取银行数据
        insertCreditCard: function () {
            const addCreditCard = this.addCreditCard;
            const cardNumId = addCreditCard.CreditCardNum;
            const cardPrice = addCreditCard.CreditCardPrice;
            const cardTime = addCreditCard.CreditCardTime;
            const bankId = addCreditCard.BankId;
            if (isEmpty(cardNumId, cardPrice, cardTime, bankId)) {
                this.tipsMessage(2, "请不要留空");
                return;
            }
            axios.post("/api/CreditCard/isLogin/AddCreditCardServlet", {
                cardNumId: cardNumId,
                cardPrice: cardPrice,
                cardTime: cardTime,
                bankId: bankId
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    return;
                }
                this.tipsMessage(data.code, data.message);
                setTimeout(() => {
                    location.reload()
                }, 500);
            })
        },
        loadMeter: async function (pageIndex) {
            const meter = this.meter;
            await axios.post("/api/CreditCard/isLogin/CreditCardNoRePayServlet", {
                pageIndex: pageIndex,
                cardNum: meter.search
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    location.reload();
                    return;
                }
                this.meter.itemsCount = data.message.itemsCount;
                this.meter.tableDatas = data.message.data;
                this.loadWebSocket();
            });
        },
        tabMeterPage: function (val) {
            this.loadMeter(val);
        },
        updateCreditCardState: function (cardId) {
            this.$confirm(
                "请确认是否标记ID" + cardId + "为还款?", {
                    confirmButtonText: '是',
                    cancelButtonText: '否',
                    type: 'warning',
                }).then(() => {
                axios.post("/api/CreditCard/isLogin/updateCreditCardStateServlet", {
                    cardId: cardId
                }).then((req) => {
                    const data = req.data;
                    if (data.code > 0) {
                        this.tipsMessage(data.code, data.error);
                        return;
                    }
                    this.tipsMessage(data.code, data.message);
                    this.loadMeter(1);
                });
            });
        },
        loadManagerCreditCard: async function (pageIndex) {
            const Manager = this.manageCreditCard;
            console.log(Manager.rangeTime);
            if (Manager.rangeTime === null) {
                this.manageCreditCard.rangeTime = ['', ''];
            }
            const minTime = Manager.rangeTime[0];
            const maxTime = Manager.rangeTime[1];
            const bankId = Manager.bankId;
            const isPay = Manager.isPay;
            const cardNum = Manager.search;

            await axios.post("/api/CreditCard/isLogin/ManagerCardServlet", {
                pageIndex: pageIndex,
                minTime: minTime,
                maxTime: maxTime,
                bankId: bankId,
                isPay: isPay,
                cardNum: cardNum
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    location.reload();
                    return;
                }
                this.manageCreditCard.tableDatas = data.message.data;
                this.manageCreditCard.itemsCount = data.message.itemsCount;
                this.loadWebSocket();
            });
        },
        tabManagerPage: function (val) {
            this.loadManagerCreditCard(val);
        },
        editUserCreditCard: async function (cardId) {

            if (isEmpty(cardId)) {
                this.tipsMessage(2, "非法传参");
                return;
            }
            await axios.post("/api/CreditCard/isLogin/getUserCreditCardMsgServlet", {
                cardId: cardId
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    return;
                }
                const mess = data.message;
                this.updateCreditCard.CreditCardId = mess.cardId;
                this.updateCreditCard.CreditCardNum = mess.cardNumId;
                this.updateCreditCard.CreditCardPrice = mess.cardPrice;
                this.updateCreditCard.CreditCardTime = mess.cardTime;
                this.updateCreditCard.BankId = mess.bankId;
                this.updateCreditCard.isPay = mess.cardState;
                this.updateCreditCard.isShow = true;
            });

        },
        updateUserCreditCard: function () {
            const updateCreditCard = this.updateCreditCard;
            const cardPrice = updateCreditCard.CreditCardPrice;
            const cardTime = updateCreditCard.CreditCardTime;
            const bankId = updateCreditCard.BankId;
            const cardState = updateCreditCard.isPay;
            const cardId = updateCreditCard.CreditCardId;
            if (isEmpty(cardPrice, cardTime, bankId, cardState, cardId)) {
                this.tipsMessage(2, "请不要留空");
                return;
            }
            axios.post("/api/CreditCard/isLogin/updateCreditCardMsgServlet", {
                cardPrice: cardPrice,
                cardTime: cardTime,
                bankId: bankId,
                cardState: cardState,
                cardId: cardId
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    return;
                }
                this.tipsMessage(data.code, data.message);
                this.updateCreditCard.isShow = false;
                this.loadManagerCreditCard(1);
            });
        },
        deleteUserCreditCard: function (cardId) {
            if (isEmpty(cardId)) {
                this.tipsMessage(2, "非法传参");
                return;
            }
            axios.post("/api/CreditCard/isLogin/deleteUserCreditCardServlet", {
                cardId: cardId
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    return;
                }
                this.tipsMessage(data.code, data.message);
                this.loadManagerCreditCard(1);

            })
        },
        loginOut: function () {
            this.$confirm(
                "是否确认退出登录?", {
                    confirmButtonText: '是',
                    cancelButtonText: '否',
                    type: 'warning',
                }).then(() => {
                axios.get("/api/User/isLogin/LoginOutServlet").then((req) => {
                    const data = req.data;
                    if (data.code > 0) {
                        this.tipsMessage(data.code, data.error);
                        return;
                    }
                    this.tipsMessage(data.code, data.message);
                    setTimeout(() => {
                        location.reload()
                    }, 500);
                });
            });
        },
        loadWebSocket: function () {
            const webSocket = new WebSocket("ws://" + window.location.hostname + ":8080/SchoolJspDemo_war_exploded/ws/CreditCardTispController");
            webSocket.addEventListener('open', function (event) {
                webSocket.send('Hello Server!');
            });
            webSocket.addEventListener('message', (event) => {
                console.log('Message from server ', event.data);
                const data = JSON.parse(event.data);
                console.log(typeof data);
                this.$notify({
                    type: 'info',
                    title: '消息提醒',
                    message: '您当前有' + data.message + "个信用卡未还款",
                    position: 'bottom-right',
                });
            });
        }
    }

    ,
    mounted() {
        const search = location.search.replace(/\?index=/, "").trim();
        if (search === "userSafe") {
            this.loadUserMsg();
            this.loadWebSocket();
        } else if (search === "manageCreditCard") {
            this.loadBankMsg();
            this.loadManagerCreditCard(1);
        } else if (search === "addCreditCard") {
            this.loadBankMsg();
            this.loadWebSocket();
        } else {
            this.loadMeter(1);

        }

    }
};
const app = Vue.createApp(App);
app.use(ElementPlus);
app.mount("#app");
